package com.cjm.one.order.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.common.result.Result;
import com.cjm.one.common.utils.AuthCustomerContextHolder;
import com.cjm.one.model.order.dto.OrderOperateDTO;
import com.cjm.one.model.order.dto.OrderStatusDto;
import com.cjm.one.model.order.entity.OrderInfo;
import com.cjm.one.model.order.enums.OrderParamsTypeEnum;
import com.cjm.one.model.order.enums.OrderStatusEnum;
import com.cjm.one.model.order.vo.OrderInfoVo;
import com.cjm.one.order.service.OrderInfoService;
import com.cjm.one.security.annotation.Within;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Tag(name = "用户订单信息")
@RequestMapping("/app/order/info")
public class AppOrderInfoController {

    @Resource
    private OrderInfoService orderInfoService;

    @GetMapping("/list")
    @Operation(summary = "用户订单信息列表")
    public Result<Page<OrderInfoVo>> list(@RequestParam(value = "current", defaultValue = "1") Long current,
                                          @RequestParam(value = "size", defaultValue = "10") Long size ,
                                          @RequestParam(value = "type", required = false, defaultValue = "0")
                                              @Parameter(description = "订单类型：0-全部,1-进行中,2-待评价,3-售后")
                                               OrderParamsTypeEnum type) {
        log.info("查询用户订单列表");

        Long id = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        log.info("查询用户id:{}的订单", id);
        Page<OrderInfoVo> page = orderInfoService.getList(current, size,type, id);
        return Result.ok(page);

    }
    @PostMapping("/add")
    @Operation(summary = "添加订单")
    public Result<String> add(@RequestBody @Validated OrderOperateDTO orderOperateDTO){
        Long id = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        orderOperateDTO.setCustomId(id);

        return Result.ok( orderInfoService.addOrder(orderOperateDTO));
    }
    @PostMapping("/update")
    @Operation(summary = "更新订单")
    public Result<Boolean> update(@RequestBody OrderInfoVo orderInfoVo){
        Long id = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        orderInfoVo.setCustomId(id);
        log.debug("更新用户id:{}的订单", id);
        return Result.ok( orderInfoService.updateOrderById(orderInfoVo));
    }
    @PostMapping("/update/status")
    @Operation(summary = "更新订单状态")
    public Result<Boolean> update(@RequestBody OrderStatusDto orderStatusDto){
        Long id = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        log.debug("更新用户id:{}的订单", id);

        return Result.ok( orderInfoService.updateByOrderStatus(orderStatusDto));
    }
    /**
     * @description: 删除订单
     * @author: cjm
     * @date: 2025/3/17 21:36
     * @param: [id]
     * @return: com.cjm.one.common.result.Result<java.lang.Boolean>
     **/
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除订单")
    public Result<Boolean> appdelete(@PathVariable Long id){
        Long userId = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        log.debug("删除用户id:{}的订单", userId);
        return Result.ok(orderInfoService.deleteOrderById(id));
    }
    /**
     * @description: 查询订单
     * @author: cjm
     * @date: 2025/3/17 21:36
     * @param: [id]
     * @return: com.cjm.one.common.result.Result<com.cjm.one.model.order.vo.OrderInfoVo>
     **/
    @GetMapping("/{id}")
    @Operation(summary = "查询订单")
    public Result<OrderInfoVo> getAppUserByOrderId(@PathVariable Long id){
        Long userId = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        log.debug("查询用户id:{}的订单", userId);
        return Result.ok(orderInfoService.getOrderInfoById(id));
    }
    @GetMapping("/ByorderNo/{orderNo}")
    @Operation(summary = "根据订单号查询")
    @Within
    public Result<OrderInfoVo> getByorderNo(@PathVariable String orderNo){
        log.debug("根据订单号查询订单:{}的订单", orderNo);
        return Result.ok(orderInfoService.selectByOrderNo(orderNo));
    }

    @PostMapping("/update/order")
    @Operation(summary = "更新订单-内部调用")
    @Within
    public Result<Boolean> updateOrder(@RequestBody OrderInfoVo orderInfoVo){
        OrderInfo orderInfo = new OrderInfo();
        BeanUtils.copyProperties(orderInfoVo, orderInfo);
        return Result.ok( orderInfoService.updateById(orderInfo));
    }


}
